<template>
    <div class="page_orderList">
        <div class="title">我的订单</div>
        <el-form :inline="true" :model="searchForm" ref="searchForm" @keyup.enter.native="search" :size="size">
            <el-form-item label="订单编号" prop="orderNO">
                <el-input v-model="searchForm.orderNO" placeholder="请输入单号" clearable></el-input>
            </el-form-item>
            <el-form-item label="出行日期" prop="tripDay">
                <el-date-picker
                        style="width: 250px"
                        v-model="searchForm.tripDay"
                        align="right"
                        unlink-panels
                        value-format="yyyy-MM-dd"
                        start-placeholder="日期">
                </el-date-picker>
            </el-form-item>
            <el-button type="primary" icon="el-icon-search" :size="size" @click="search">查询</el-button>
            <el-button @click="resetSearch" icon="el-icon-refresh" :size="size">重置</el-button>
        </el-form>

        <div class="tableBox">
            <el-table :data="dataList" border style="width: 100%;" height="100%">
                <el-table-column prop="orderNo" header-align="center" align="center" label="订单编号" min-width="100"></el-table-column>
                <el-table-column header-align="center" align="center"
                                 min-width="100" label="旅游产品编号">
                    <template slot-scope="{row}">
                        <span style="color: #3a8ee6; cursor: pointer" @click="goInfoDetail(row)">{{row.infoId}}</span>
                    </template>
                </el-table-column>
                <el-table-column header-align="center" align="center"
                                 prop="title"
                                 min-width="100" label="旅游产品名称"></el-table-column>
                <el-table-column header-align="center" align="center"
                                 prop="status"
                                 width="100" label="订单状态"></el-table-column>
                <el-table-column prop="createTime" header-align="center" align="center" label="下单时间" width="115"></el-table-column>
                <el-table-column prop="tripDay" header-align="center" align="center" label="出行日期" width="115"></el-table-column>
                <el-table-column prop="realPay" header-align="center" align="center" label="金额" width="100"></el-table-column>
                <el-table-column prop="discount" header-align="center" align="center" label="折扣" width="50"></el-table-column>
                <el-table-column fixed="right" header-align="center" align="center"
                                 width="158" label="操作">
                    <template slot-scope="{row}">
                        <el-button type="text" size="small" @click="addAppraise(row)">评论</el-button>
                        <el-button type="text" size="small" style="color: orangered" @click="cancelOrder(row)">退单</el-button>
                        <el-button type="text" size="small" @click="payOrder(row)">支付</el-button>
                        <el-button type="text" size="small" style="color: orangered" @click="deleteOrder(row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <pagination v-show="total>0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize" @pagination="getDataList" />
        <DialogAppraise :dialogAppraiseShow.sync="dialogAppraiseShow" :propInfo="propInfo"></DialogAppraise>
    </div>
</template>

<script>
  import {getOrderList, deleteOrder,payOrder, cancelOrder} from '@/api/order'
  import { mapGetters } from 'vuex'
  import Pagination from '@/components/Pagination'
  import DialogAppraise from './components/DialogAppraise'
  export default {
    name: 'orderList',
    components: {
      Pagination,
      DialogAppraise
    },
    data() {
      return {
        dataList: [],
        searchForm: {
          orderNO: '',
          tripDay: '',
          pageNum: 1,
          pageSize: 10,
        },
        total: 0,
        propInfo: {},
        dialogAppraiseShow: false
      }
    },
    computed: {
      ...mapGetters(['size']),
    },
    created() {
        this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        getOrderList(this.searchForm).then((res) => {
          let {data, total} = res
          this.dataList = data
          this.total = parseInt(total)
        }).catch((res) => {
        })
      },
      // 重置
      resetSearch () {
        this.$refs['searchForm'].resetFields()
      },
      // 查询
      search () {
        this.searchForm.pageNum = 1
        this.getDataList()
      },
      addAppraise(row) {
        this.propInfo = row
        this.dialogAppraiseShow = true
      },
      goInfoDetail(row) {
        let {infoId} = row
        this.$router.push({
          name: 'travelDetail',
          query: {
            infoId: infoId
          }
        })
      },
      // 删除订单
      deleteOrder(row) {
        deleteOrder({orderId: row.orderId}).then(res => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      },
      payOrder(row) {
        this.$confirm(`支付${row.realPay}元，即可开启随心浪漫的旅行, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info'
        }).then(() => {
          payOrder({orderId: row.orderId}).then(res => {
            this.$message.success('支付成功')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消支付'
          });
        });
      },
      // 取消订单
      cancelOrder(row) {
        this.$prompt('请输入退单理由', '提示', {
          inputType: 'textarea',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          cancelOrder({orderId: row.orderId, applyContext: value}).then(res => {
            this.$message.success('提交成功，等待审核即可')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });
        });
      }
    }
  }
</script>

<style lang="scss">
    .page_orderList {
        padding: 20px;
        height: calc(100% - 50px);
        display: flex;
        flex-direction: column;
        box-sizing: border-box;
        .title {
            font-size: 28px;
            padding-bottom: 10px;
            font-weight: bold;
        }
        .tableBox {
            flex: 1;

        }
    }
</style>
